<!DOCTYPE html>
<html class="x-admin-sm">
{block name="include"}{include file="head" /}{/block}

  <body>
    <div class="layui-fluid">
        <div class="layui-row">
            <div class="layui-form-item">
                现在正在为<span class="x-red">{$data.role_name}</span>分配权限
            </div>
            <form class="layui-form layui-form-pane">
                <input type="hidden" name=" role_id" value="{$data.id}">
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">
                        拥有权限
                    </label>

                    <table  class="layui-table layui-input-block" style="width: 100%">

                        <tbody>
                        {foreach $top_auth as $top_v}

                            <tr>
                                <td style="width: 10%">
                                    <input class="top_auth" type="checkbox" lay-filter="top_auth" name="id[]"  lay-skin="primary"  value="{$top_v.id}" {in name="top_v.id" value="$data.role_auth_ids"} checked="checked"{/in} >{$top_v.auth_name}

                                </td>
                                <td style="width: 90%">
                                    <div class="layui-input-block">
                                        {foreach $second_auth as $second_v}
                                         {if ($second_v.pid eq $top_v.id)}


                                        <input class="second_auth" name="id[]" lay-skin="primary" lay-filter="second_auth" type="checkbox"  value="{$second_v.id}" {in name="second_v.id" value="$data.role_auth_ids"} checked="checked" {/in} > {$second_v.auth_name}


                                        {/if}
                                        {/foreach}
                                    </div>
                                </td>
                            </tr>
                        {/foreach}
                        </tbody>
                    </table>
                </div>

                <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="add">修改</button>
              </div>
            </form>
        </div>
    </div>


    <script>
        layui.use(['form','layer'], function(){
            $ = layui.jquery;
          var form = layui.form
          ,layer = layui.layer;

          form.on('checkbox(top_auth)',function (data) {
              var status = $(this).prop('checked');

              $(this).parent().siblings('td').find('.second_auth').prop('checked',status);
              form.render();
              //$(this).closest('td').find('.second_auth').prop('checked',status)
              
          });

            //监听提交
            form.on('submit(add)', function(data) {
                $.ajax({
                    url: "{:url('admin/role/saveauth')}",
                    type:'post',
                    data:data.field,
                    success:function (callback) {
                        if (callback.msg == 200){
                            layer.msg('修改角色权限成功',{icon:6});
                            setTimeout(function () {
                                xadmin.close();
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            },1200);
                        }else {
                            layer.msg("修改角色权限失败", {icon: 6})
                        }

                    }
                });
                return false;
            });

        });
    </script>

  </body>
  <script>
      $(function () {
          //顶级权限 选中效果


         // $(".top_auth").click(function () {

              //  console.log(index)
              //var status = $(this).prop('checked');
              // console.log(status);

              //将当前行权限 设置为顶级权限状态一直
              // $(this).closest('td').find('.second_auth').prop('checked',status)
         // });
          //二级权限
          $('.second_auth').change(function () {
              var checked = $(this).closest('td').find('.second_auth:checked');
              var status = checked.length>0;
              $(this).closest('td').find('.top_auth').prop('checked',status);
          });
      });

  </script>

